<?php

namespace App\Http\Controllers\Admin;

use App\User;
use App\Writing;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Spatie\Permission\Models\Role;

class UserController extends Controller
{
    public function index(Request $request){
      //  dd(Auth::user()->getAllPermissions());
       // Auth::user()->can('LoginController@showLoginForm');
        $searchKey=$request->searchKey;
        $searchTime=$request->searchTime;

       return view('admin.user.list',compact('searchKey','searchTime'));
    }
    public function ajax_list(Request $request){
        $user=User::with('roles');
        if($request->searchKey){
            $user=$user->where('name','like', '%'.$request->searchKey.'%')
                ->orWhere('email','like', '%'.$request->searchKey.'%');
        }
        if($request->searchTime&&$request->searchTime!=''){
            $timeArray=explode('~',$request->searchTime);
            $start_time=trim($timeArray[0]);
            $end_time=trim($timeArray[1]);
            $user=$user->whereBetween('created_at',[$start_time,$end_time]);
        }
         $user=$user->paginate($request->limit)
            ->toArray();

        return $this->page_json_render($user);
    }
    //屏蔽所有作品
    public function display(Request $request,$id){
        $user=User::findOrFail($id);
        $user->display_writing=$request->display;
        $user->save();
        //屏蔽作品本身
        Writing::where('author_id',$id)
            ->update(['is_display'=>$request->display]);

        return $this->success();
    }
    //增加
    public  function  add(Request $request){
        $role=Role::where('guard_name','web')->get();
        return view('admin.user.edit_add',compact('id','role'));
    }
    public  function store(Request $request){
       // dd($request->all());
        $this->validate($request,[
            'name'=>'required',
            'email'=>'required|email|unique:users',
            'password'=>'required',
        ]);
        $user=new User();
        $user->email=$request->email;
        $user->name=$request->name;
        $user->avatar=$request->avatar;
        $user->password=bcrypt($request->password);
        $user->save();
        if($request->role) {
           $user->assignRole($request->role);
         }
        return redirect()->route('admin.user_index')->with(
            $this->success()
        );
    }
    //修改
    public function edit(Request $request,$id){
        $role=Role::where('guard_name','web')->get();
        $model=User::findOrFail($id);

        return view('admin.user.edit_add',compact('id','role','model'));
    }
    public function update(Request $request){
        $this->validate($request,[
            'name'=>'required',
        ]);

       // dd($request->all());
        $user=User::findOrFail($request->id);
        $user->name=$request->name;
        if($request->password){
            $user->password=bcrypt($request->password);
        }

        //登陆
        if(isset($request->allow_login)){

            $user->allow_login=0;
        }else{
            $user->allow_login=1;
        }
        //评论
        if(isset($request->allow_comment)){
            $user->allow_comment=0;
        }else{
            $user->allow_comment=1;
        }
        $user->save();
        if($request->role!=''){
           // dd(111);
            $user->syncRoles($request->role);
        }else{

            $user->syncRoles();
        }
        return redirect()->route('admin.user_index')->with(
            $this->success()
        );
    }



    //修改密码
    public function modify_password(){
        return view('admin.user.modifyPassword');
    }
    //修改密码提交
    public function modify_password_post(Request $request){
        $this->validate($request,[
            'password'=>'required|confirmed',
        ]);
        $user=Auth::user();
        $user->password=bcrypt($request->password);
        $user->save();
        return redirect()->back()->with(
         $this->success()
        );
    }
    //修改admin个人信息
    public function user_info(){
        $userInfo=Auth::user();
        return view('admin.user.userInfo',compact('userInfo'));

    }

    //提交修改个人信息
    public function user_info_post(Request $request){

        $userInfo=Auth::user();
        $userInfo->avatar=$request->avatar;
        $userInfo->save();
        return redirect()->back()->with(
            $this->success()
        );
    }
}
